﻿using HslCommunication;
using HslCommunication.Profinet.Omron;
using System;
using System.IO;
using System.Text;

namespace NDK.AcqDevice
{
    public class OmronPLC
    {
        private OmronCipNet omronCipNet = null;
        public bool ConnectStatus = false;
        public OmronPLC()
        {

        }

        public bool Connect(string IP)
        {
            if (!Authorization.SetAuthorizationCode("12a99167-05ff-4c89-936f-93d42033f882"))
                return false;
            omronCipNet = new OmronCipNet(IP);
            OperateResult operateResult = omronCipNet.ConnectServer();
            ConnectStatus = operateResult.IsSuccess;
            return operateResult.IsSuccess;
        }


        public bool DisConnect()
        {
            OperateResult operateResult = omronCipNet.ConnectClose();
            ConnectStatus = false;
            return operateResult.IsSuccess;
        }

        public bool Write(string address, short[] value)
        {
            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool Writebools(string address, bool[] value)
        {
            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteDouble(string address, double value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, float.Parse(value.ToString()));

                return operateResult.IsSuccess;
            }
            return false;
        }


        public bool WriteDouble(string address, double[] values)
        {
            if (ConnectStatus)
            {
                int[] re = new int[values.Length];
                for (int i = 0; i < re.Length; i++)
                {
                    re[i] = Convert.ToInt32(values[i] * 1000);
                }
                OperateResult operateResult = omronCipNet.Write(address, re);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteBool(string address, bool value)
        {
            bool result = false;
            //Stopwatch stopwatch = new Stopwatch(); stopwatch.Start();
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            //writeLog($"写bool值耗时:{stopwatch.ElapsedMilliseconds}", "PLC_PC通讯");
            //Console.WriteLine($"写bool值耗时:{stopwatch.ElapsedMilliseconds}");
            return result;
        }

        public bool ReadBool(string address)
        {
            bool result = false;
            //Stopwatch stopwatch = new Stopwatch(); stopwatch.Start();
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult<bool> operateResult = omronCipNet.ReadBool(address);
                return operateResult.Content;
            }

            return result;
        }

        public string ReadString(string address, ushort length = 20)
        {
            string String = "";
            if (address == null || address == "XXX")
            {
                return String;
            }

            if (ConnectStatus)
            {
                OperateResult<string> operateResult = omronCipNet.ReadString(address, length, Encoding.ASCII);
                return operateResult.Content;
            }
            else
            {
                return String;
            }
        }

        public bool[] ReadBoolArray(string address, ushort length)
        {
            if (ConnectStatus)
            {
                var operateResult = omronCipNet.ReadBool(address, length);
                return operateResult.Content;
            }
            return null;
        }
        public float ReadFloat(string address)
        {
            float flo = 0;
            if (address == null || address == "XXX")
            {
                return flo;
            }

            if (ConnectStatus)
            {
                OperateResult<float> operateResult = omronCipNet.ReadFloat(address);
                return operateResult.Content;
            }
            else
            {
                return flo;
            }
        }

        public bool WriteFloats(string address, float[] value)
        {
            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public string ReadBit(string address)
        {
            string res = "False";

            if (address == null || address == "XXX")
            {
                return "False";
            }


            if (ConnectStatus)
            {
                if (address.Contains("."))
                {
                    string zfAddr = address.Split('.')[0];
                    OperateResult<short> operateResult = omronCipNet.ReadInt16(zfAddr);

                    string str = Convert.ToString(operateResult.Content, 2);


                    if (str.Length == 16)
                    {
                        string str2 = str.Substring(1, 1);
                        if (int.Parse(str2) == 1)
                        {
                            res = "True";
                        }
                    }
                    else if (str.Length == 15)
                    {
                        res = "True";
                    }
                }
            }
            return res;
        }
        public int ReadInt32(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<int> operateResult = omronCipNet.ReadInt32(address);
                return operateResult.Content;
            }
            return 0;
        }
        public int ReadInt16(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<ushort> operateResult = omronCipNet.ReadUInt16(address);
                return operateResult.Content;
            }
            return 0;
        }

        public bool WriteInt16(string address, Int16 value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        public bool WriteShort16s(string address, short[] value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }
            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }
        public bool WriteString(string address, string value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronCipNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

        static readonly object obj = new object();
        public void writeLog(string str, string DirFileName, bool IsShow = false)
        {
            lock (obj)
            {
                FileInfo fi = new FileInfo($"D:\\Log\\{DirFileName}\\{DateTime.Now.ToString("yyyyMMdd")}.txt");
                if (!Directory.Exists(fi.DirectoryName))
                {
                    Directory.CreateDirectory(fi.DirectoryName);
                }

                using (StreamWriter sw = new StreamWriter(fi.FullName, true))
                {
                    sw.WriteLine(DateTime.Now.ToString() + ":     " + str);
                    if (IsShow)
                    {
                        sw.WriteLine("---------------------------------------------------------");
                    }
                    sw.Flush();
                    sw.Close();
                }
            }
        }
    }
}
